package com.oocl.work1_and_work2;

//public class homeWork1 {
//	public int getRandom(int x, int y, int a, double b){
//		int m = 1000000;
//		int w = (int)(Math.random()*(y - x + 1)) + x;
//		
//		if(w == a) return w;     				  //产生a直接返回
//		
//		int sex = (int)(Math.random()*m) + y;     //为了防止重叠区间，让sex产生的随机数落在x,y区间外面
//		
//		//概率计算
//		double p = 1.0/(y-x+1);
//		double k = (b - p) / (1 - p);            
//		
//		int mm = (int)(k * m + y);				  //为了防止重叠区间避开x,y区间	
//		if(sex <= mm)
//			return a;
//		return w;
//	}
//	
//	public static void main(String[] args) {
//		double count = 0;
//		int runTime = 10000000;          		//运行的次数
//		homeWork1 work1 = new homeWork1();
//		for(int i = 0; i < runTime; i++){
//			int a = 5;
//			if(a == work1.getRandom(1,10,5,0.3)){
//				count++;
//			}
//		}
//		System.out.println(count/runTime);	//输出概率
//	}
//}

public class HomeWork1 {
	public int getRandom(int x, int y, int a[], double b[]){
		int m = 100000;
		int w = (int)(Math.random()*(y - x + 1)) + x;
		double p = 1.0/(y-x+1);
		int container = -1;
		
		double tt[] = new double[a.length];
		int[] mm = new int[a.length];
		
		int sex = (int)(Math.random()*m) + y;
		
		for(int k = 0; k < a.length; k++){	
			container = -1;
			if(w == a[k]){								//产生a直接返回
				return a[k];
			}
			
			tt[k] = (b[k] - p) / (1 - p);   			//计算概率
			mm[k] = (int)(tt[k] * m);
		}
			
		for(int i = 0; i < a.length; i++){	
			int tmp = y;								//为了防止重叠区间避开x,y区间
			for(int j = 0; j < a.length; j++){
				tmp += mm[j];
				if(sex <= tmp){
					container = a[j];
					break;
				}
			}
			
			if(container != -1 ) return container;
		}
		
		return w;
	}
	
	public static void main(String[] args) {
		double count = 0;
		int runTime = 100000000;          		//运行的次数
		int a[] ={5,3};
		double b[] = {0.3,0.5};
		HomeWork1 work1 = new HomeWork1();
		for(int i = 0; i < runTime; i++){
			int a1 = 5;
			int b1 = 3;
//			if(a1 == work1.getRandom(1,10,a,b)){
//				count++;
//			}
			if(b1 == work1.getRandom(1,10,a,b)){
				count++;
			}
		}
		System.out.println(count/runTime);	//输出概率
	}
}